package csc.fresher.view;

import java.io.IOException;
import java.util.List;

import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.commons.BankingConstant;
import csc.fresher.entity.DepositTransaction;
import csc.fresher.entity.SystemUser;
import csc.fresher.entity.TransactionState;
import csc.fresher.service.AccountService;
import csc.fresher.service.DepositAccountService;
import csc.fresher.service.DepositAccountTransactionService;
import csc.fresher.service.SystemUserService;

@Component
@Scope(value = "request")
public class ApprovedTransactionManageBean {
	@Autowired
	private DepositAccountTransactionService depositAccountTransactionService;	
	@Autowired
	private SystemLoginManageBean systemLoginManageBean;	
	@Autowired
	private SystemUserService systemUserService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private DepositAccountService depositAccountService;
	
	private List<DepositTransaction> listApprovedTransactions;
	private SystemUser systemUser;
	
	
	public void onLoad() {
		if (systemLoginManageBean.getSystemUser() == null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("/csc_final/templates3/Logon.jsf");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			boolean result = checkRole(listrole);
			if (result == false) {
				try {
					FacesContext.getCurrentInstance().getExternalContext()
							.redirect("/csc_final/Group1/AccountManagement.xhtml");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public boolean checkRole(List<Long> listRole) {

		for (int i = 0; i < listRole.size(); i++) {
			if (BankingConstant.ROLE_ACCOUNT_ADMIN == listRole.get(i)) {
				return true;
			}
		}
		return false;
	}	
	
	public SystemUser getSystemUser() {
		return systemUser;
	}

	public void setSystemUser(SystemUser systemUser) {
		this.systemUser = systemUser;
	}
	
	public List<DepositTransaction> getListApprovedTransactions() {
		searchListApprovedTransactions();
		return listApprovedTransactions;
	}
	
	public void setDepositAccountTransactionService(
			DepositAccountTransactionService depositAccountTransactionService) {
		this.depositAccountTransactionService = depositAccountTransactionService;
	}

	public void setDepositAccountService(
			DepositAccountService depositAccountService) {
		this.depositAccountService = depositAccountService;
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}


	public String searchListApprovedTransactions() {
		listApprovedTransactions = depositAccountTransactionService
				.getDepositTransactionByState(TransactionState.DONE);
		return "listApprovedTransactions";
	}
}
